// addrAditor.js
var app = getApp()
Page({

  data: {
  },
  // 完成按钮
  confirmClick : function(){
  

    var thiz = this

    var domain = app.globalData.domain
    var addrUpdateUrl = app.globalData.addrUpdateInterface
    var cid = app.globalData.cid
    var address_id = thiz.data.address_id
    var kui_key = app.globalData.kui_key
    var addrUpdate = domain + addrUpdateUrl;
    var user_id = app.globalData.inv_i

    var teleStatus = thiz.data.teleStauts
    // var zipcodeStatus = thiz.data.zipcodeStatus
    setTimeout(function(){
      if (thiz.data.address.address_name != "" && thiz.data.address.tel != "" && thiz.data.address.address != ""){
        thiz.setData({
          hiddenLoading: false,
          focus: false
        })
        if (address_id) {
          wx.request({
            url: addrUpdate,
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            data: {
              address_id: thiz.data.address_id,
              address_name: thiz.data.address.address_name,
              tel: thiz.data.address.tel,
              // zip_code: thiz.data.address.zip_code,
              address: thiz.data.address.address,
              model : "MINI",
              user_id: user_id
            },
            success: function (res) {
              thiz.setData({
                hiddenLoading: true
              })
              if (res.data.status == 1) {
                if(thiz.data.ren == 'ren'){
                  wx.redirectTo({
                    url: '../addrSelect/addrSelect?address_id=' + res.data.data.address_id + '&addr=' + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
                  })
                }else{
                  if (thiz.data.type == "2") {
                    wx.redirectTo({
                      url: '../cartConfirm/cartConfirm?address_id=' + res.data.data.address_id + '&addr=' + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
                    })
                  } else {
                    wx.redirectTo({
                      url: '../addrManage/addrManage',
                    })
                  }
                }
               
              } else {
                wx.showToast({
                  title: res.data.errMsg,
                })
              }
            }
          })
        } else {


          wx.request({
            url: addrUpdate,
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            data: {
              address_id: 'null',
              address_name: thiz.data.address.address_name,
              tel: thiz.data.address.tel,
              // zip_code: thiz.data.address.zip_code,
              address: thiz.data.address.address,
              model: "MINI",
              user_id: user_id
            },
            success: function (res) {
              thiz.setData({
                hiddenLoading: true
              })
              if(res.data.status == 1){
                if (thiz.data.type == "2") {
                  wx.redirectTo({
                    url: '../cartConfirm/cartConfirm?address_id=' + res.data.data.address_id + '&addr=' + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
                  })
                } else {
                  wx.redirectTo({
                    url: '../addrManage/addrManage',
                  })
                }
              }else{
                wx.showToast({
                  title: res.data.errMsg,
                })
              }
            }
          })
        }
      }else{
        wx.showToast({
          title: '请完善信息',
        })
      }
      
      
    }, 400)
  },
  //地址验证
  validateAddr : function(e){
    var thiz = this
    this.data.address.address = e.detail.value

    thiz.setData({
      address: this.data.address
    })
  },
  //姓名验证
  validateName : function(e){
    var thiz = this
    this.data.address.address_name = e.detail.value

    thiz.setData({
      address: this.data.address
    })
  },
  //邮政编码验证
  // validateZipcode : function(e){
  //   var value = e.detail.value
  //   var zipcodeStatus = true;
  //   var thiz = this

  //   var re = /^[1-9][0-9]{5}$/
  //   if (re.test(value)){
  //     zipcodeStatus = true
  //   }else {
  //     wx.showToast({
  //       title: '邮编错误',
  //       icon: 'success',
  //       duration: 1500
  //     })
  //     zipcodeStatus = false
  //   }

  //   this.data.address.zip_code = e.detail.value

  //   this.setData({
  //     address: this.data.address,
  //     zipcodeStatus: zipcodeStatus
  //   })
  // },
  //地区选择
  bindRegionChange : function(e){
    var region = e.detail.value

    console.log(region)

    this.data.address.area = region[0] + region[1] + region[2]

    this.setData({
      address: this.data.address,
    })

    console.log()


  },
  //手机号码验证
  validatemobile: function (e) {
     var mobile = e.detail.value
     var teleStatus = true;
     var thiz = this

     if(mobile.length != 11) {

       wx.showToast({
         title: '手机号长度有误！',
         icon: 'success',
         duration: 1500
       })

       teleStatus = false;
     }

     var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
     if (!myreg.test(mobile)) {
        wx.showToast({
          title: '手机号有误！',
          icon: 'success',
          duration: 1500
        })
        teleStatus = false;
     }

     thiz.data.address.tel = mobile

     thiz.setData({
       address: thiz.data.address,
       teleStauts: teleStatus
     })
    
    
  },
  onShow: function(){
   
  },
  onLoad: function (options) {
    var thiz = this
    var background_color = app.globalData.background_color
    
    var address_id = ""
    if(options && options.address_id){
      address_id = options.address_id
    }

    var url = ''
    if (options && options.url) {
      url: options.url
    }

    if (options.pay_goods && options.totalPrice && options.postGoods) {
      thiz.setData({
        postGoods: options.postGoods,
        pay_goods: options.pay_goods,
        totalPrice: options.totalPrice
      })
    }

    var domain = app.globalData.domain
    var addrInfoUrl = app.globalData.addrInfoInterface
    var cid = app.globalData.cid
    var kui_key = app.globalData.kui_key
    var user_id = app.globalData.inv_i

    var addrInfo = domain + addrInfoUrl;
    if (address_id != "") {
      wx.request({
        url: addrInfo,
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: {
          format_type: 'json',
          api_cate: 'Personal',
          api: 7003,
          address_id: address_id,
          model: "MINI",
          user_id: user_id
        },
        success: function (res) {
          thiz.setData({
            address : res.data.data,
            hiddenLoading: true
          })
        }
      })
    }else{
      thiz.setData({
        address : {
          tel : '',
          details : '',
          area : '请选择地区',
          full_name : '',
          zip_code : ''
        },
        hiddenLoading: true
      })
    }

    //获取系统信息
    var thiz = this;

    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          type : options.type,
          url: url,
          address_id: address_id,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
        });
      }
    })
  }
})